<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div>
  <div>
    <div fxFlex="70">
      <form (keydown.enter)="false" (ngSubmit)="patch()">
        <div class="gv-form-section">
          <div class="gv-form-section-title">
            <h5>MFA</h5>
            <small>Multi-factor Authentication adds an additional factor to the login process in order to grant user
              access.</small>
            <mat-divider></mat-divider>
          </div>
          <div fxLayout="column">
            <div *ngIf="hasFactors()">
              <ngx-datatable class="material"
                             [columnMode]="'flex'"
                             [headerHeight]="40"
                             [footerHeight]="40"
                             [rowHeight]="50"
                             [rows]='factors'>
                <ngx-datatable-column name="Factors" [flexGrow]="4">
                  <ng-template let-row="row" ngx-datatable-cell-template>
                    {{row.name}}
                  </ng-template>
                </ngx-datatable-column>
                <ngx-datatable-column name="" [flexGrow]="1">
                  <ng-template let-row="row" ngx-datatable-cell-template>
                   <span style="display: flex; align-items: center;">
                    <mat-icon>{{getFactorTypeIcon(row.factorType)}}</mat-icon>
                    <small style="margin-left: 5px;">{{displayFactorType(row.factorType)}}</small>
                  </span>
                  </ng-template>
                </ngx-datatable-column>
                <ngx-datatable-column name="" [flexGrow]="1">
                  <ng-template let-row="row" ngx-datatable-cell-template>
                    <mat-slide-toggle (change)="selectFactor($event, row.id)"
                                      [checked]="isFactorSelected(row.id)" [disabled]="!editMode">
                    </mat-slide-toggle>
                  </ng-template>
                </ngx-datatable-column>
              </ngx-datatable>
            </div>

            <div *ngIf="!hasFactors()" style="font-size: 14px; text-align: center;">
              <p>No multi-factor authentication has been set up.</p>
            </div>
          </div>
        </div>

        <div *ngIf="hasFactors() && hasSelectedFactors()" class="gv-form-section">
          <div class="gv-form-section-title">
            <div fxLayout="row" style="align-items: center;">
              <h5>Adaptive MFA</h5>
              <button mat-icon-button (click)="openAMFADialog($event)">
                <mat-icon>info_outline</mat-icon>
              </button>
            </div>
            <small>Adaptive MFA skips Multi-factor authentication based on the user's information and enforces factors
              otherwise.</small>
            <blockquote class="warning">
              <p>
                <mat-icon>warning_outline</mat-icon> You need to install a <b> GeoIP service </b> plugin to use the geoip based variables.
              </p>
            </blockquote>
            <mat-divider></mat-divider>
          </div>
          <mat-form-field appearance="outline" floatLabel="always">
            <input matInput placeholder="Adaptive MFA rule" name="adaptiveMfaRule" [(ngModel)]="adaptiveMfaRule"
                   (ngModelChange)="formChanged = true;"/>
            <mat-hint>
              MFA challenge page will be displayed if the condition above is <b>false</b> in addition to the login step.
            </mat-hint>
          </mat-form-field>
        </div>

        <div *ngIf="hasFactors() && hasSelectedFactors()" class="gv-form-section">
          <div class="gv-form-section-title">
            <div fxLayout="row" style="align-items: center;">
              <h5 style="margin: 0;">Step up authentication</h5>
              <button mat-icon-button (click)="openStepUpDialog($event)">
                <mat-icon>info_outline</mat-icon>
              </button>
            </div>
            <small>Step up authentication may be required in certain situations where it is asked to provide access to a
              resource that is determined to be very sensitive.</small>
            <mat-divider></mat-divider>
          </div>
          <div fxLayout="column">
            <mat-form-field appearance="outline" floatLabel="always">
              <input matInput placeholder="Step up authentication rule" name="mfaSelectionRule"
                     [(ngModel)]="mfaStepUpRule" (ngModelChange)="formChanged = true;">
              <mat-hint>MFA challenge page will be displayed if the conditions are met in addition of the login step.
              </mat-hint>
            </mat-form-field>
          </div>
        </div>

        <div *ngIf="hasFactors() && hasSelectedFactors()" class="gv-form-section">
          <div class="gv-form-section-title">
            <div fxLayout="row" style="align-items: center;">
              <h5>Remember Device</h5>
            </div>
            <small>Remember Device will skip MFA if the user's device is known.</small>
            <blockquote class="warning" *ngIf="!hasDeviceIdentifierPlugins()">
              <p>
                <mat-icon>warning_outline</mat-icon> You need to create a <b> device identifier </b> plugin to use the remember device feature.
              </p>
            </blockquote>
            <mat-divider></mat-divider>
          </div>
          <div class="gv-form-section-description">
            <mat-slide-toggle matInput placeholder="Step up authentication rule" name="mfaRememberDevice"
                              (change)="changeActive()" [checked]="rememberDevice.active" [disabled]="!hasDeviceIdentifierPlugins()">
              Enable Remember Device
            </mat-slide-toggle>
          </div>
          <br />
          <form *ngIf="rememberDevice.active">
            <mat-form-field appearance="outline" floatLabel="always" style="margin-left: 5px;">
              <mat-label>Select your device identifier provider</mat-label>
              <mat-select (selectionChange)="updateDeviceIdentifierId($event)" [(value)]="rememberDevice.deviceIdentifierId" name="deviceIdentifierId">
                <mat-option *ngFor="let rd of deviceIdentifiers; index as i;" [value]="rd.id">{{rd.name}}</mat-option>
              </mat-select>
            </mat-form-field>
            <mat-form-field appearance="outline" floatLabel="always" fxFlex="35" style="margin-left: 5px;">
              <input matInput type="number" placeholder="Duration" name="expirationTimeSeconds" min="0" [min]="0"
                     [value]="displayExpiresIn()"
                     (change)="onExpiresInEvent($event)">
            </mat-form-field>
            <mat-form-field appearance="outline" floatLabel="always" fxFlex="40" style="margin-left: 5px;">
              <mat-select placeholder="Unit" name="expirationTimeUnit" [value]="displayUnitTime()"
                          (selectionChange)="onUnitTimeEvent($event)">
                <mat-option value="seconds">SECONDS</mat-option>
                <mat-option value="minutes">MINUTES</mat-option>
                <mat-option value="hours">HOURS</mat-option>
                <mat-option value="days">DAYS</mat-option>
                <mat-option value="weeks">WEEKS</mat-option>
                <mat-option value="months">MONTHS</mat-option>
                <mat-option value="years">YEARS</mat-option>
              </mat-select>
              <mat-hint>Period of time users can trust devices</mat-hint>
            </mat-form-field>
          </form>
        </div>

        <div fxLayout="row" style="margin-top: 20px;" *hasPermission="['application_factor_update']">
          <button mat-raised-button color="primary" type="submit" [disabled]="!formChanged">SAVE</button>
        </div>
      </form>
    </div>
    <div class="gv-page-description" fxFlex>
      <h3>Multi-factor Authentication</h3>
      <div class="gv-page-description-content">
        <p>By selecting a MFA authentication method, the end-user will be prompted to process the MFA verification after
          he has been authenticated.</p>
      </div>
    </div>
  </div>
</div>
